<?php

/* * ************************************************************************* 
 * global.func.php
 * Encoding : UTF-8
 * Desc. :模块公共函数库
 * Created on : 2013-5-14 18:01:11 by Aiden , aiden.ke@doosnet.net
 * Updated on : 2013-5-14 18:01:11 by Aiden , aiden.ke@doosnet.net
 * ************************************************************************* */

/**
 * 获取价格、销量、评分、库存信息
 * @param output $string 页面源码
 * @param row $array 对应数组
 * @param checkstock $boolean 是否检查库存
 * @return int
 */
function get_infos($output, $row, $checkstock=FALSE) {
    $price = $reviews = $score = 0;
    $stock = 1;

    //价格
    $price = pet_str("price:'", "'", $output);
    if (!$price) {
        $price_url = "http://jprice.360buy.com/price/np" . $row['goods_id'] . "-TRANSACTION-J.html";
        $price_data = get_web($price_url, '', '', $row['goods_url']);
        $price = pet_str('"amount":', ',', $price_data['buffer']);
    }
    $price = initprice($price) * 100;

    //销量
    $url = 'http://club.jd.com/productpage/p-' . $row['goods_id'] . '-s-0-t-3-p-0.html?_=' . time();
    $pf_data = get_web($url, '', '', $row['goods_url']);
    $reviews = pet_str('"commentCount":', ',', $pf_data['buffer']);

    //评分
    if ($reviews) {
        $score = pet_str('"goodRateShow":', ',', $pf_data['buffer'], 'block');
        $score = round($score / 2);
    }

    //检查库存
    if ($checkstock) {
        $skuidkey = pet_str("skuidkey:'", "'", $output);
        if ($skuidkey) {
            //provinceid=19为广东 1为北京 2为上海 12为江苏 13为山东
            $provinceid_arr = array(19, 1, 2);
            foreach ($provinceid_arr as $k_id => $v_id) {
                $stock_url = "http://price.360buy.com/stocksoa/StockHandler.ashx?callback=getProvinceStockCallback&type=provincestock&skuid=" . $skuidkey . "&provinceid=" . $v_id;
                $stock_data = get_web($stock_url);
                $attr = pet_str('"StockState":', ',', $stock_data['buffer']);
                if ($attr == 33) {
                    break;
                }
                unset($stock_url, $stock_data);
            }

            //库存状态 34为无货 33为有货 36预订
            if (in_array($attr, array(0, 34, 18))) {
                $stock = 0;
            }
        }
    }
    unset($price_url, $price_data, $url, $pf_data, $skuidkey, $stock_url, $stock_data);
    return array('price' => $price, 'reviews' => $reviews, 'score' => $score, 'stock' => $stock);
}

/**
 * 判断商品状态
 * @param output $string 页面源码
 * @param row $array 对应数组
 * @return int
 */
function is_onsale($output, $row) {
    $attr = 1;
    //判断是否有货的值
    if (str_exists($output, '该商品已下柜，非常抱歉！')) {
        $attr = 0;
    }
    return $attr;
}

?>
